package com.jsmile.cloud.accountcenter.shop.feign;

import javax.validation.Valid;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jsmile.cloud.accountcenter.api.req.ReqMemberDetial;
import com.jsmile.cloud.accountcenter.api.req.shop.ReqShopDelete;
import com.jsmile.cloud.accountcenter.api.req.shop.ReqShopQuery;
import com.jsmile.cloud.accountcenter.api.res.ResShop;
import com.jsmile.cloud.accountcenter.shop.service.ShopApplicationService;
import com.jsmile.mail.security.controller.AuthcController;
import com.jsmile.mail.security.handler.JsmileContext;
import com.jsmile.mall.api.JSmileResult;
import com.jsmile.mall.api.annotation.CurrentContext;
import com.jsmile.mall.api.to.JsmilePage;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
@AllArgsConstructor
public class ShopServiceApiImpl extends AuthcController {

    private final ShopApplicationService shopApplicationService;

    @RequestMapping(value = "/feign/shop/getShop", method = RequestMethod.POST)
    public JSmileResult getShop(@RequestBody @Valid ReqShopDelete reqShopDelete, @CurrentContext JsmileContext jsmileContext) {
        ResShop shop = shopApplicationService.getShop(reqShopDelete.getShopId());
        return JSmileResult.success(shop);
    }

    @RequestMapping(value = "/feign/shop/getShopByUserId", method = RequestMethod.POST)
    public JSmileResult getShopByUserId(@RequestBody @Valid ReqMemberDetial reqMemberDetial, @CurrentContext JsmileContext jsmileContext) {
        ResShop shop = shopApplicationService.getShopByUserId(reqMemberDetial.getUserId());
        return JSmileResult.success(shop);
    }

    @RequestMapping(value = "/feign/shop/getShopPageList", method = RequestMethod.POST)
    public JSmileResult<JsmilePage<ResShop>> getShopPageList(@RequestBody @Valid ReqShopQuery reqShopQuery, @CurrentContext JsmileContext jsmileContext) {
        reqShopQuery.setCurTenantId(jsmileContext.getTenantId());
        IPage<ResShop> pages = shopApplicationService.getShopPageList(reqShopQuery);
        return JSmileResult.success(new JsmilePage(reqShopQuery.getPageIndex(), reqShopQuery.getPageSize(), pages.getTotal(), pages.getRecords()));
    }

}
